<!-- EditRemarkModal.vue -->
<template>
  <BasicModal
    v-bind="$attrs"
    width="80%"
    :footer="false"
    @register="registerModal"
    :title="t('sys.script.Distributionlog')"
    @ok="handleOK"
  >
    <a-table
      @change="handleTableChange"
      :pagination="pagination"
      :loading="loading"
      bordered
      :scroll="{ x: 1200 }"
      :columns="columns"
      :dataSource="dataSource"
    >
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'status'">
          <a-tag v-if="record.status == 1" color="green"> {{ t('sys.configuration.normal') }} </a-tag>
          <a-tag v-if="record.status == 1" color="red"> {{ t('sys.configuration.blockUp') }} </a-tag>
        </template>
        <template v-if="column.dataIndex === 'exceptionStatus'">
          <a-tag v-if="record.exceptionStatus == 1" color="green"> 分发成功 </a-tag>
          <a-tag v-if="record.exceptionStatus == 0" color="red"> 分发失败 </a-tag>
          <a-tag v-if="record.exceptionStatus == 2" color="orange"> 未分发 </a-tag>
        </template>
        <template v-if="column.dataIndex === 'downloadStatus'">
          <a-tag v-if="record.downloadStatus == 1" color="green"> 已下载 </a-tag>
          <a-tag v-if="record.downloadStatus == 0" color="red"> 未下载 </a-tag>
        </template>
        <template v-if="column.dataIndex === 'taskprocess3'">
          <a-tag> 未处理 </a-tag>
        </template>
        <template v-if="column.dataIndex === 'taskprocess4'">
          <a-tag> 已完成 </a-tag>
        </template>
        <template v-if="column.dataIndex === 'action'">
          <a-space>
            <a-button type="link">重试</a-button>
          </a-space>
        </template>
      </template>
    </a-table>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { onMounted, ref } from 'vue';
  import { Input } from 'ant-design-vue';
  import { BasicModal, useModalInner } from '@/components/Modal';
  import { updateRemarkAPI } from '@/api/demo/script';
  import { postUserListPage } from '@/api/demo/device';
  import { useI18n } from "@/hooks/web/useI18n";

  const { t } = useI18n();
  // 假设你有一个更新备注的接口
  const emit = defineEmits(['success']);
  const remark = ref('');
  const record = ref<any>(null); // 用于存储传递的记录
  const columns = ref([
    {
      title: t('sys.script.columns.deiveName'),
      dataIndex: 'deviceName',
      width: 100,
    },
    {
      title: t('sys.script.columns.deviceCode'),
      dataIndex: 'deviceCode',
      width: 100,
    },
    {
      title: 'MAC',
      dataIndex: 'mac',
      width: 100,
    },
    {
      title: t('sys.analysis.columns.status'),
      dataIndex: 'status',
      width: 100,
    },
    {
      title: t('sys.script.columns.exceptionStatus'),
      dataIndex: 'exceptionStatus',
      width: 100,
    },
    {
      title: t('sys.script.columns.downloadStatus'),
      dataIndex: 'downloadStatus',
      width: 100,
    },
    {
      title: t('sys.script.columns.creadTime'),
      dataIndex: 'creadTime',
      width: 100,
    },
    {
      title: t('sys.script.columns.endTime'),
      dataIndex: 'endTime',
      width: 100,
    },
    {
      title: t('sys.configuration.action'),
      dataIndex: 'action',
      width: 100,
    },
  ]);
  const dataSource = ref([
    {
      id: 1,
      deviceName: '设备1',
      deviceCode: '18822181882773',
      mac: 'MAC-18822181882773',
      status: '1',
      exceptionStatus: '2',
      downloadStatus: '0',
      creadTime: '2025-11-1/14:12',
      endTime: '2025-11-1/19:12',
    },
    {
      id: 2,
      deviceName: '设备2',
      deviceCode: '18822181882773',
      mac: 'MAC-18822181882773',
      status: '0',
      exceptionStatus: '0',
      downloadStatus: '1',
      creadTime: '2025-11-1/14:12',
      endTime: '2025-11-1/19:12',
    },
    {
      id: 3,
      deviceName: '设备3',
      deviceCode: '18822181882773',
      mac: 'MAC-18822181882773',
      status: '1',
      exceptionStatus: '1',
      downloadStatus: '0',
      creadTime: '2025-11-1/14:12',
      endTime: '2025-11-1/19:12',
    },
    {
      id: 4,
      deviceName: '设备4',
      deviceCode: '18822181882773',
      mac: 'MAC-18822181882773',
      status: '1',
      exceptionStatus: '2',
      downloadStatus: '0',
      creadTime: '2025-11-1/14:12',
      endTime: '2025-11-1/19:12',
    },
  ]);
  const loading = ref(true);
  const pagination = ref({
    total: 0,
    current: 1,
    pageSize: 10,
  });
  const handleTableChange = (e) => {
    pagination.value = e;
    init();
  };
  const init = () => {
    loading.value = false;
  };
  onMounted(() => {
    init();
  });
  // 注册弹窗
  const [registerModal, { closeModal }] = useModalInner((data) => {
    record.value = data.record; // 存储传递的记录
    remark.value = data.record.remark || ''; // 初始化备注
  });

  async function handleOK() {
    try {
      // // 调用接口更新备注
      // await updateRemarkAPI({ id: record.value.id, remark: remark.value });
      // emit('success'); // 触发 success 事件
      closeModal(); // 关闭弹窗
    } catch (error) {
      console.error('重新下发失败:', error);
    }
  }
</script>
